import javax.swing.*;
import java.awt.event.*;
import java.net.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

import java.awt.Dimension;

public class SIPTalkUI extends javax.swing.JApplet {

	/**
	* Auto-generated main method to display this 
	* JApplet inside a new JFrame.
	*/
	SIPTalk sipTalk;
	JPanel panel = null;
	JDialog callConfirmDialog = null;
	String fromURI;
	String media;
	
	public void init() {
		LoginPanel loginPanel = new LoginPanel(this);
		this.loadPanel("login");
	}
	
	public SIPTalkUI() {
		super();
		initGUI();
		try {
            sipTalk = SIPTalk.getInstance(this, InetAddress.getByName("localhost"), 7777);
        } catch(Exception e) { e.printStackTrace(); }
	}
	
	public void loadPanel(String type) {
		if(panel != null) {
			this.remove(panel);
		}
		if(type.equals("login")) {
			this.repaint();
			LoginPanel loginPanel = new LoginPanel(this);
			panel = loginPanel;
			this.add(loginPanel);
			loginPanel.revalidate();
			loginPanel.repaint();
			loginPanel.setVisible(true);
		} else if(type.equals("dialer")) {
			this.repaint();
			DialerPanel dialerPanel = new DialerPanel(this);
			panel = dialerPanel;
			this.add(dialerPanel);
			dialerPanel.revalidate();
			dialerPanel.repaint();
			dialerPanel.setVisible(true);
		}
	}
	
	private void initGUI() {
		try {
			setSize(new Dimension(350, 350));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    public void errorMessage(boolean exit, String message, String title) {
        JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
        if(exit == true) {
            sipTalk.stop();
        }
    }
    
    public void callConfirmDialog(String from, String media) {
    	this.fromURI = from;
    	this.media = media;
    	JButton acceptButton = new JButton("Accept");
    	JButton rejectButton = new JButton("Reject");
    	Object[] options = {acceptButton, rejectButton};
    	JOptionPane optionPane = new JOptionPane("Incoming call from " + from, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION,
    												null, options, options[0]);
    	JFrame frame = new JFrame();
        callConfirmDialog = optionPane.createDialog(frame, "Incoming Call");
        callConfirmDialog.setModal(false);
        callConfirmDialog.setVisible(true);
        
        acceptButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callConfirmDialog.setVisible(false);
                sipTalk.accept(fromURI);
                DialerPanel dialerPanel = (DialerPanel) panel;
                dialerPanel.acceptCall();
            }
        });
        rejectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                callConfirmDialog.setVisible(false);
                sipTalk.reject(fromURI);
                DialerPanel dialerPanel = (DialerPanel) panel;
                dialerPanel.acceptCall();
            }
        });
        callConfirmDialog.addWindowListener(new WindowAdapter() {
        	public void windowClosing(WindowEvent e) {
        		sipTalk.reject(fromURI);
        	}
        });
    }
    
    public void removeConfirmDialog() {
    	callConfirmDialog.setVisible(false);
    	sipTalk.uaState = sipTalk.REGISTERED;
    }
}
